import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/main_page/home/detail/page/author_note.dart';
import 'package:flutter_douban/main_page/home/detail/views/author_top_view.dart';
import 'package:flutter_douban/main_page/home/detail/views/intro_view.dart';
import 'package:flutter_douban/model/cook_info_model.dart';
import 'package:flutter_douban/tool_views/my_underline_tabIndicator.dart';
import 'author_collection.dart';
import 'author_cook_page.dart';

const List<Tab> _tabs = [
  Tab(text: '  菜谱(12)  '),
  Tab(text: '  笔记  '),
  Tab(text: '  收藏  '),
];

// final List<Widget> _tabaContent = [
//   AuthorCookPage(),
//   AuthorNotePage(),
//   AuthorCollectionPage(),
// ];

class AuthorPage extends StatefulWidget {
  final CookInfoModel model;
  const AuthorPage({Key? key,required this.model}) : super(key: key);

  @override
  _AuthorPageState createState() => _AuthorPageState();
}

class _AuthorPageState extends State<AuthorPage> with TickerProviderStateMixin {
  late TabController _tabController;
  ScrollController _scrollController = ScrollController();

  /// 初始化方法 请求数据
  @override
  void initState() {
    super.initState();

    _tabController = TabController(
      vsync: this,
      length: _tabs.length,
      initialIndex: 0, // 默认选中哪一个
    );

    _tabController.addListener(() {
      _tabController.index;
      // print('切换了item');
    });

    ///监听滚动位置
    _scrollController.addListener((){
      if(_scrollController.position.pixels > 100){
        // print('${_scrollController.position}');
      }
    });
  }

  @override
  void dispose() {
    // 释放对象
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: NestedScrollView(
          controller: _scrollController,
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                backgroundColor: Colors.white,
                expandedHeight: 294.0,
                pinned: true,
                flexibleSpace: Stack(
                  children: [
                    IntroView(model: widget.model.a!,),
                    Positioned(
                        child: AuthorTopView(model: widget.model.a!,)
                    ),
                    // Positioned(
                    //   top: kToolbarHeight + 19,
                    //   child: Container(
                    //     height: 1,
                    //     width: MediaQuery.of(context).size.width,
                    //     color: AppColors.danger,
                    //   ),
                    // )
                  ],
                ),
              ),
              SliverPersistentHeader(
                pinned: true,
                delegate: StickyTabBarDelegate(
                  child: TabBar(
                    // isScrollable: true,
                    unselectedLabelStyle: TextStyle(
                      fontSize: 16,
                    ),
                    labelStyle:
                    TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    indicator: MyUnderlineTabIndicator(
                        borderSide:
                        BorderSide(width: 3.0, color: AppColors.yellow)),
                    controller: this._tabController,
                    tabs: _tabs,
                  ),
                ),
              ),
            ];
          },
          body: TabBarView(
            controller: this._tabController,
            children: [
              AuthorCookPage(id: int.parse(widget.model.a!.id!)),
              AuthorNotePage(id: int.parse(widget.model.a!.id!),),
              AuthorCollectionPage(id: int.parse(widget.model.a!.id!),),
            ],
          ),
        ),
      ),
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Colors.white,
      child: this.child,
    );
  }

  @override
  double get maxExtent => this.child.preferredSize.height;

  @override
  double get minExtent => this.child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
